/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
import i18n from '@/utils/i18n'
export function parseTime(time, cFormat) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string')) {
      if ((/^[0-9]+$/.test(time))) {
        // support "1548221490638"
        time = parseInt(time)
      } else {
        time = time.replace(new RegExp(/-/gm), '/')
      }
    }

    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return [i18n.t('message.num7'), i18n.t('message.num1'), i18n.t('message.num2'), i18n.t('message.num3'), i18n.t('message.num4'), i18n.t('message.num5'), i18n.t('message.num6')][value ] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option) {
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return i18n.t('message.gangan')
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + i18n.t('message.fzq')
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + i18n.t('message.xsq')
  } else if (diff < 3600 * 24 * 2) {
    return i18n.t('message.tyq')
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return (
      d.getMonth() +
      1 +
      i18n.t('message.yue') +
      d.getDate() +
      i18n.t('message.ren2') +
      d.getHours() +
      i18n.t('message.shi2') +
      d.getMinutes() +
      i18n.t('message.fen2')
    )
  }
}

/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
  const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  if (!search) {
    return {}
  }
  const obj = {}
  const searchArr = search.split('&')
  searchArr.forEach(v => {
    const index = v.indexOf('=')
    if (index !== -1) {
      const name = v.substring(0, index)
      const val = v.substring(index + 1, v.length)
      obj[name] = val
    }
  })
  return obj
}

export function setTempDate() {
  // 获取时间
  var myDate = new Date()
  return (
    myDate.getFullYear() +
    '-' +
    (myDate.getMonth() + 1) +
    '-' +
    myDate.getDate() +
    ' ' +
    myDate.getHours() +
    ':' +
    myDate.getMinutes() +
    ':' +
    myDate.getSeconds()
  )
}
export function getFMdate(number, separator) {
  const n = parseInt(number)
  const nd = new Date()
  const day = nd.getDate()
  const setd = new Date(nd.setDate(day + n))
  const ryear = setd.getFullYear()
  const rmonth = setd.getMonth() + 1
  const rday = setd.getDate()
  let frmonth, frday
  // month day + '0'
  if (rmonth < 10) {
    frmonth = '0' + rmonth
  } else {
    frmonth = rmonth
  }
  if (rday < 10) {
    frday = '0' + rday
  } else {
    frday = rday
  }
  // separator
  const sep = separator || '-'
  return ryear + sep + frmonth + sep + frday
}
export function parseTimeb(num) {
  const start = new Date()
  start.setTime(start.getTime() - 3600 * 1000 * 24 * num)
  return parseTime(new Date(start))
}
export function newRule(rule, value, callback) {
  if (!value[0] || !value[1]) {
    callback(new Error(rule.message || i18n.t('message.qsr') + i18n.t('message.jwd')))
  } else if (!/^[+-]?(0|([1-9]\d*))(\.\d+)?$/.test(value[0]) || !/^[+-]?(0|([1-9]\d*))(\.\d+)?$/.test(value[1])) {
    callback(new Error(rule.message || i18n.t('message.qsrzrjwd')))
  } else {
    callback()
  }
}
export function validateMoney(rule, value, callback) {
  if (!value) {
    callback(new Error(i18n.t('message.jebnwk')))
  } else if (value.indexOf('.') !== -1 && value.split('.').length > 2) {
    callback(new Error(i18n.t('message.qsrzrgsdje'))) // 防止输入多个小数点
  } else if (value.indexOf('.') !== -1 && value.split('.')[1].length > 2) {
    callback(new Error(i18n.t('message.qsrzrdxsws'))) // 小数点后两位
  } else {
    callback()
  }
}
